package org.example.springservermodule;

import lombok.extern.slf4j.Slf4j;
import org.example.springservermodule.Bean.PersonBean;
import org.example.springservermodule.Bean.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@Slf4j
public class PersonController {
    @Autowired
    private PersonService personServer;
//   @CrossOrigin("*")
     @RequestMapping("/insert")
    public ResultBean insert(@RequestBody PersonBean personBean) {
       log.info("insert 请求参数：{}",personBean);
         try {
            personServer.insert(personBean);
            log.info("insert 请求成功");
            return new ResultBean("success",200 , null);
        } catch (Exception e) {
             log.info("insert 请求失败");
            return new ResultBean("fail"+e.getMessage(),500 , null);
        }
    }
    @PostMapping("/update")
    public ResultBean update(@RequestBody PersonBean personBean) {
        log.info("update 请求参数：{}",personBean);
        try {
            personServer.update(personBean);
            return new ResultBean("success",200 , null);
        }catch (Exception e){
            log.info("update 请求失败");
            return new ResultBean("fail"+e.getMessage(),500 , null);
        }
    }
    @GetMapping("/select")
    public ResultBean select(@RequestParam("id") int id) {
        log.info("select 请求参数：{}",id);
        try {
            Object result = personServer.selectById(id);
            return new ResultBean("success",200 , result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
